<?php
/*
	申请成为代理商
	如果已申请，但尚未审核通过，则页面显示请耐心等待管理员审核！
	如果已申请，且审核通过，则显示审核通过，并提供按钮前往代理中心
	如果审核不通过，则显示审核不通过原因
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	$buymid=intval($_RQ['buymid']);
	$merchid=intval($_RQ['merchid']);
	if($buymid){
		$this->session('buymid',$buymid);
	}
	if($merchid){
		$this->session('merchid',$merchid);
	}
	$buymid=$buymid?:$_SESSION['uid'];
	$merchid=$merchid?:$_SESSION['onemerchid'];
	$peiset=$this->pdo_get('peiset',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$buymid,'merchid'=>$merchid));
	$peiset['address']=json_decode($peiset['address'],true);
	$return['peiset']=$peiset['id']?$peiset:array('days'=>'','mid'=>$buymid,'merchid'=>$merchid);
	$return['peiset']['goods']=json_decode($return['peiset']['goods'],true);
	$return['allgoods']=$this->model('order')->get_allgoods($buymid,$merchid);
	foreach($return['allgoods'] as $k=>&$av){
		$goodsset=$this->model('model')->check_goodsset($av);
		$av['numset']=json_decode($goodsset['numset'],true);
		if($av['lastnum']<=0){
			unset($return['allgoods'][$k]);
		}
	}
	//用户地址
	$order=pdo_get('jy_weishop_order',array('mid'=>$buymid,'merchid'=>$merchid,'ispay'=>1,'is_dingpei'=>1),array('address','sendtype','addressid','is_dingpei'),'id desc');
	$order['address']=json_decode($order['address'],true);
	if($order['address'] && ($order['address']['name'] || $order['address']['realname'])){
		$address=$order['address'];
	}else{
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
	}
	if(!is_array($address['pcd'])){
		$address['pcd']=json_decode($address['pcd'],true);
	}
	$return['address']=$address;
	if($_SESSION['onemerchid']){
		$return['yuesetting']=$this->plugin_setting('yuesetting'.$_SESSION['onemerchid']);
		$return['yuyuexieyi']=$this->plugin_setting('yuyuexieyi'.$_SESSION['onemerchid']);
	}else{
		$return['yuesetting']=$this->plugin_setting('yuesetting');
		$return['yuyuexieyi']=$this->plugin_setting('yuyuexieyi');
	}
	$weekarray=array('日','一','二','三','四','五','六');
	$no_weeks=$return['yuesetting']['no_weeks'];
	foreach($weekarray as $k=>$v){
		if(strpos($no_weeks,$k.';') !== false){
			$isno=1;
		}else{
			$isno=0;
		}
		$work_weekarray[]=array(
			'id'=>$k,
			'title'=>$v,
			'isno'=>$isno,
		);
	}
	$return['params']=array(
		'nowmonth'=>date('Y-m'),
		'work_weekarray'=>$work_weekarray,
	);
	$return['currentyear'] =$_RQ['year']?:date('Y');
	$return['currentmonth'] = $_RQ['month']?:date('m');
	$return['gai_peihour']=$this->peisetting['gai_peihour'];
	set_html('title','设置配送');
	exi($return);
}
if($_RQ['op']=='get_work_time'){
	$daytime=date('Y-m-d');
	if($_SESSION['onemerchid']){
		$yuesetting=$this->plugin_setting('yuesetting'.$_SESSION['onemerchid']);
	}else{
		$yuesetting=$this->plugin_setting('yuesetting');
	}
	$send1_timebox=$yuesetting['send1_timebox'];
	$key0=strtotime($daytime.' 12:00');
	$key1=strtotime($daytime.' 18:00');
	$key2=strtotime($daytime.' 24:00');
	$times0=array();
	$times1=array();
	$times2=array();
	foreach($send1_timebox as &$sv){
		$sv['key']=strtotime($daytime.' '.$sv['times']['start']);
		if($sv['key']<$key0){
			$times0[] = array(
				'start' => $sv['times']['start'],
				'end' => $sv['times']['end'],
				'timestamp' => $sv['key'],
				'timev' =>$sv['times']['start'].'~'.$sv['times']['end'],
			);
		}
		if($sv['key']>=$key0 && $sv['key']<$key1){
			$times1[] = array(
				'start' => $sv['times']['start'],
				'end' => $sv['times']['end'],
				'timestamp' => $sv['key'],
				'timev' =>$sv['times']['start'].'~'.$sv['times']['end'],
			);
		}
		if($sv['key']>=$key1){
			$times2[] = array(
				'start' => $sv['times']['start'],
				'end' => $sv['times']['end'],
				'timestamp' => $sv['key'],
				'timev' =>$sv['times']['start'].'~'.$sv['times']['end'],
			);
		}
	}
	$work_time=str_array($store['work_time']);
	//门店营业时间
	//设置日期
	$data=array();
	$weekarray=array('日','一','二','三','四','五','六'); //先定义一个数组
	$weekid=date("w");
	$newweekarray=array();
	for($i=1;$i<8;$i++){
		$weekid=date("w",strtotime(date("Y-m-d",strtotime("+".$i." day"))));
		$newweekarray[]=$weekarray[$weekid];
	}
	for($i=1;$i<15;$i++){
		$weekid=date("w",strtotime(date("Y-m-d",strtotime("+".$i." day"))));
		if(count($work_time)){
			$iswork=strpos($store['work_time'],';'.$weekid.';')?1:0;
		}else{
			$iswork=1;
		}
		$data[]=array(
			'riqi'=>date("Y-m-d",strtotime("+".$i." day")),
			'day'=>date("d",strtotime("+".$i." day")),
		);
	}
	$h=date('H');
	if($h>22){
		$nowkey=3;
	}elseif($h>18){
		$nowkey=2;
	}elseif($h>12){
		$nowkey=1;
	}else{
		$nowkey=0;
	}
	$return=array(
		'days'=>$data,
		'nowtime'=>time(),
		'nowriqi'=>$data[0]['riqi'],
		'nowkey'=>0,
		'hours0'=>$times0,
		'hours1'=>$times1,
		'hours2'=>$times2,
		'weekarray'=>$newweekarray
	);
	exi($return);
}
if($_RQ['op']=='time_days'){
	$buymid=$this->session('buymid');
	$merchid=$this->session('merchid');
	$buymid=$buymid?:$_SESSION['uid'];
	$merchid=$merchid?:$_SESSION['onemerchid'];
		$year =$_RQ['year']?:date('Y');
		$month = $_RQ['month']?:date('m');
		$day = date("t",strtotime($year.'-'.$month));
		//$day = cal_days_in_month(CAL_GREGORIAN, $month,$year);
		$calendar = array();
		$i = 1;
		while ($i <= $day) {
				$key=$i;
				if ($i < 10) {
					$i = '0' . $i;
				}
				$date=$year . '-' . $month . '-' . $i;
				if(strtotime($date)<time()){
					$islast=1;
				}else{
					$islast=0;
				}
				//商品
				$sendlog=pdo_get('jy_peimilk_sendlog',array('status'=>0,'day'=>$date,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'buymid'=>$buymid,'is_dingpei'=>1));
				$goods=$sendlog['goods']?json_decode($sendlog['goods'],true):array();
				$isgoods=0;
				foreach($goods as &$gv){
					$gv['newtitle']=$gv['title'];
					if($gv['leititle'] || $gv['selespename']){
						$gv['newtitle']=$gv['leititle'].'-'.$gv['selespename'];
					}
					$gv['newtitle']=cfc('tools')->substr($gv['newtitle'],0,5);
					$isgoods=$islast?0:1;
				}
				$calendar[] = array(
					'i'=>$i,
					'day'=>$date,
					'islast'=>$islast,
					'goods'=>$goods,
					'isgoods'=>$isgoods
				);
				++$i;
		}
		$return['calendar'] = $calendar;
		$weekday=$year . '-' . $month . '-01';
		$return['week'] = date('w', strtotime($weekday));
		$return['kongday']=array();
		for($i=0;$i<$return['week'];$i++){
			$return['kongday'][]=$i;
		}
		
		$years = array();
		$i = 0;
		while ($i <= 10) {
			$years[] = $year + $i;
			++$i;
		}
		$months = array('01','02','03','04','05','06','07','08','09','10','11','12');
		$return['months']=$months;
		$return['years']=$years;
		$return['year']=$year;
		$return['month']=$month;
		exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	$data=array(
		'set_mid'=>$_SESSION['uid'],
		'days'=>trim($post['days']),
		'times'=>trim($post['times']),
		'address'=>ijson_encode($post['address']),
	);
	if($post['id']){
		$data['uptime']=time();
		$this->update('peiset',$data,array('id'=>$post['id']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$post['mid'];
		$data['merchid']=$post['merchid'];
		$data['order_id']=0;
		$data['createtime']=time();
		$this->insert('peiset',$data);
	}
	exi('提交成功','','goback');
}
if($_RQ['op']=='postgoods'){
	$post=get_postdata();
	$goods=$post;
	$goodsset=$this->model('model')->check_goodsset($goods);
	$this->model('order')->upallgoodssendlog($goodsset['mid'],$goodsset['merchid']);
	exi('提交成功');
}
if($_RQ['op']=='upstop'){
	$post=get_postdata();
	$peiset=$post;
	$this->update('peiset',array('stopdays'=>$peiset['stopdays']),array('id'=>$post['id']));
	$this->model('order')->upallgoodssendlog($peiset['mid'],$peiset['merchid'],$peiset['stopdays']);
	//$this->model('order')->upstopdays($peiset['stopdays'],$peiset['mid'],$peiset['merchid']);
	exi('提交成功');
}